<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
        autoResize: {
            type: Boolean,
            default: true
        }
    },
    data () {
        return {
            chart: null
        }
    },
    beforeDestroy () {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart (chartData, xAxisData, lineName) {
            this.chart = echarts.init(this.$el, 'macarons')
            this.chart.resize()
            this.setOptions({ chartData, xAxisData, lineName })
        },
        setOptions ({ chartData, xAxisData, lineName } = {}) {
            this.chart.setOption({
                xAxis: {
                    data: xAxisData,
                    boundaryGap: false,
                    axisTick: {
                        show: false
                    }
                },
                grid: {
                    left: 10,
                    right: 40,
                    bottom: 10,
                    top: 40,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    axisTick: {
                        show: false
                    }
                },
                series: [{
                    name: lineName,
                    itemStyle: {
                        normal: {
                            color: '#3888fa',
                            lineStyle: {
                                color: '#3888fa',
                                width: 2
                            }
                        }
                    },
                    smooth: true,
                    type: 'line',
                    data: chartData,
                    markPoint: {
                        data: [
                            { type: 'max', name: '最大值' },
                        ],
                        symbolSize: 40
                    },
                    animationDuration: 2800,
                    animationEasing: 'cubicInOut'
                }]
            })
        }
    }
}
</script>
